package com.example.prison.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.AlarmManagement;
import com.example.prison.service.AlarmManagementService;
import com.example.prison.service.SystemLogService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api("报警管理")
@RestController
@RequestMapping("/api")
public class AlarmManagementController {

    @Autowired
    private AlarmManagementService alarmManagementService;

    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation("获取报警等级列表")
    @GetMapping("/getAlarm")
    public Response<PageInfo<AlarmManagement>> getAlarm(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        log.info("获取报警等级列表");
        PageInfo<AlarmManagement> pageInfo = alarmManagementService.get(pageNumber, pageSize);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增报警等级")
    @PostMapping("/addAlarm")
    public Response<?> addAlarm(@RequestBody AlarmManagement alarm) {
        log.info("新增报警等级");
        systemLogService.add("新增报警等级");
        int result = alarmManagementService.add(alarm);
        if (result == -1) {
            return Response.error("报警等级已存在！");
        }
        return Response.success("添加成功！");
    }

    @ApiOperation("删除报警等级")
    @PostMapping("/deleteAlarm")
    public Response<?> deleteAlarm(@Parameter(description = "报警等级ID") @RequestBody Integer[] ids) {
        log.info("删除报警等级");
        systemLogService.add("删除报警等级");
        alarmManagementService.delete(ids);
        return Response.success("删除成功！");

    }

    @ApiOperation("编辑报警等级")
    @PostMapping("/updateAlarm")
    public Response<?> updateAlarm(@RequestBody AlarmManagement alarm) {
        log.info("编辑报警等级");
        systemLogService.add("编辑报警等级");
        Integer id = alarm.getId();
        int result = alarmManagementService.update(alarm, id);
        if (result == -1) {
            return Response.error("更新失败！报警等级已存在！");
        } else {
            return Response.success("更新成功！");
        }
    }

    @ApiOperation("依据id获取报警等级列表")
    @GetMapping("/getAlarmById")
    public Response<AlarmManagement> getAlarmById(@Parameter(description = "报警等级ID")@RequestParam(value = "alarmId") Integer alarmId) {
        log.info("依据id获取报警等级列表");
        systemLogService.add("依据id获取报警等级列表");
        AlarmManagement alarm = alarmManagementService.getById(alarmId);
        if(ObjectUtils.isEmpty(alarm)) {
            return Response.error("此报警等级id不存在！");
        }
        return Response.success("获取成功！", alarm);
    }

    @ApiOperation("依据报警等级获取报警等级列表")
    @GetMapping("/getAlarmByLevel")
    public Response<PageInfo<AlarmManagement>> getAlarmByLevel(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                      @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                      @Parameter(description = "报警等级")@RequestParam(value = "alarmLevel") String alarmLevel) {
        log.info("依据报警等级获取报警等级列表");
        systemLogService.add("依据报警等级获取报警等级列表");
        PageInfo<AlarmManagement> pageInfo = alarmManagementService.getLevel(pageNumber, pageSize, alarmLevel);
        if(ObjectUtils.isEmpty(pageInfo.getList())) {
            return Response.error("此报警等级不存在！");
        }
        return Response.success("获取成功！", pageInfo);
    }

}
